<?php
/**
 * Created by PhpStorm.
 * User: yafex
 * Date: 2021/3/16
 * Time: 11:16
 */
class model_wishFlow extends L {

    /**
     * 描述 : 获取图表数据
     * 作者 : Zoro.Zhu
     */
    public static function getAccountChart($params){
        $weekDate = !empty($params['timeS']) ? $params['timeS'] : date('Y-m-d', strtotime("-10 day"));
        $endTime = !empty($params['timeE']) ? $params['timeE'] : date('Y-m-d', strtotime("-2 day"));
        $type = !empty($params['type']) ? $params['type'] : 'total_impressions'; //默认查询产品曝光量
        $typeName = model_com::getVsConfig('wishChartType');
        $account = model_com::getAccount('wish');
        $sql = "SELECT
            `date`,
            account,
            `data` ->> '$.{$type}' as qty
        FROM
            `wish_flow` 
        WHERE
            account IN ('" . join("','", $account) . "') ";
        $sql .= " AND `date` >= '{$weekDate}' ";
        $sql .= " AND `date` <= '{$endTime}' ";
        $sql .= " ORDER BY account";
        $day = L::sql($sql);

        //赋值横坐标
        for ($i = strtotime($weekDate); $i <= strtotime($endTime); $i = $i + 86400) {
            $xAxis[] = date("Y-m-d", $i);
        }
        $data = [];
        foreach($day as $r){
            $data[$r['date']][$r['account']] = $r['qty'];
        }
        //赋值纵坐标
        foreach ($xAxis as $xAxisTime) {
            foreach($account as $a){
                $series[$a][] = !empty($data[$xAxisTime][$a]) ? $data[$xAxisTime][$a] : 0;
            }
        }
        $s = [];
        $legendData = [];
        foreach($series as $a => $d){
            $s[] = ["name" => $a, 'type' => 'line', 'data' => $d];
            $legendData[] = $a;
        }
        $chart = [
            'title' => $typeName[$type] . '图表',
            //纵坐标
            'legendData' => $legendData,
            //横坐标
            'xAxis' => $xAxis,
            'series' => $s,
        ];
        return $chart;
    }

}